﻿using Architecture.API.Application.Command.ProvinceCommand;
using Architecture.Domain;
using Architecture.Repository.Interface;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--省
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ProvinceManagementController : ControllerBase
    {
        /// <summary>
        /// 省仓储接口
        /// </summary>
        protected readonly IProvinceRepository _provinceRepository;

        /// <summary>
        /// 中介者接口
        /// </summary>
        protected readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="provinceRepository">省仓储接口</param>
        /// <param name="mediator">中介者接口</param>
        public ProvinceManagementController(IProvinceRepository provinceRepository, IMediator mediator)
        {
            _provinceRepository = provinceRepository;
            _mediator = mediator;
        }

        /// <summary>
        /// 获取省列表
        /// </summary>
        /// <returns>省列表</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<List<Province>>> GetProvinces()
        {
            ApiResult<List<Province>> result = new ApiResult<List<Province>>();
            try
            {
                result.Data = await _provinceRepository.GetAllAsync();
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;           
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取省列表--MediatR模式
        /// </summary>
        /// <returns>省列表</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<List<Province>>> Search(ProvinceSearchCommand command)
        {
            ApiResult<List<Province>> result = new ApiResult<List<Province>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }


    }
}
